package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.PageParams;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 添加员工
     * @param category
     * @return
     */
    @Override
    public int save(Category category) {
        int rows = categoryMapper.insert(category);
        return rows;
    }

    /**
     * 分页查询
     * @param pageParams
     * @return
     */

    @Override
    public IPage<Category> selectPage(PageParams pageParams) {
        IPage<Category> categoryPage = new Page<>(pageParams.getPage(), pageParams.getPageSize());
        categoryMapper.selectPage(categoryPage,null);
        return categoryPage;
    }

    /**
     * 删除员工
     * @param categoryId
     * @return
     */
    @Override
    public int deleteCategory(Long categoryId) {
      return categoryMapper.deleteById(categoryId);
    }

    /**
     * 更新员工
     * @param category
     * @return
     */
    @Override
    public int update(Category category) {
        int rows = categoryMapper.updateById(category);
        return rows;
    }

    @Override
    public List<Category> list(Category category) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.eq(category.getType()!=null,Category::getType,category.getType());
        //添加排序条件
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryMapper.selectList(lqw);
        return list;
    }
}
